import matplotlib.pyplot as plt
import pandas as pd
import numpy as np

#绘制微生物数量统计图
def savePic(excelPath,figurePath):
    plt.rcParams["font.sans-serif"] = ["SimHei"]  # 设置字体
    plt.rcParams["axes.unicode_minus"] = False  # 正常显示负号

    df = pd.read_excel(excelPath + './CountResult.xlsx')

    y = df.iloc[0, 1:].values
    x = ['颗粒污泥', '累枝虫', '絮体', '轮虫', '丝状菌']
    plt.xticks(range(len(y)), x)

    plt.title('微生物数量统计')
    plt.xlabel('种类')
    plt.ylabel('数量')
    for i, v in enumerate(y):
        plt.text(i, v, str(v), ha='center', va='bottom')
    plt.bar(range(len(y)), y)

    # # set size
    # maxsize = 80
    # m = 0.3
    # N = len(x)
    # s = maxsize / plt.gcf().dpi * N + 2 * m
    # margin = m / plt.gcf().get_size_inches()[0]
    #
    # plt.gcf().subplots_adjust(left=margin, right=1. - margin)
    # plt.gcf().set_size_inches(s, plt.gcf().get_size_inches()[1])

    plt.savefig(figurePath)
    plt.close()



def savePicMulti(epath, empath, figurePath):
    plt.rcParams["font.sans-serif"] = ["SimHei"]  # 设置字体
    plt.rcParams["axes.unicode_minus"] = False  # 正常显示负号


    data = pd.read_excel(epath)

    name = ['pic_date', 'Granular sludge', 'Epistylis', 'Floc', 'Rotifer', 'Filamentous bacteria']  # 指定行列名

    mydata = pd.DataFrame(data=data, columns=name)

    mydata_mean = mydata.groupby('pic_date').mean().reset_index()  # 对第一列值相同的行，对其每一列的数据分别求平均值，求完后只保留每列平均值这一行

    mydata_mean.to_excel(empath,
                         index=False)  # 将数据存到新的excel表格中

    mydataT = np.transpose(mydata_mean)
    print(mydataT)

    x = mydata_mean['pic_date']
    y1 = mydata_mean['Granular sludge']
    y2 = mydata_mean['Epistylis']
    y3 = mydata_mean['Floc']
    y4 = mydata_mean['Rotifer']
    y5 = mydata_mean['Filamentous bacteria']


    plt.plot(x, y1, c='black', marker='o', linestyle='-', label='Granular sludge')
    plt.plot(x, y2, c='red', marker='o', linestyle='-', label='Epistylis')
    plt.plot(x, y3, c='chocolate', marker='o', linestyle='-', label='Floc')
    plt.plot(x, y4, c='gold', marker='o', linestyle='-', label='Rotifer')
    plt.plot(x, y5, c='olivedrab', marker='o', linestyle='-', label='Filamentous bacteria')


    font1 = {'family': 'Times New Roman', 'weight': 'normal', 'size': 10}
    plt.xticks(fontproperties='Times New Roman', fontsize=10)
    plt.yticks(fontproperties='Times New Roman', fontsize=10)

    plt.xlabel(u'Date', font1)
    plt.ylabel(u'Number', font1)

    plt.legend(loc=2, prop=font1)  # 图例展示位置，数字代表第几象限

    # change x internal size
    plt.gca().margins(x=0)
    plt.gcf().canvas.draw()

    # set size
    maxsize = 80
    m = 0.3
    N = len(x)
    s = maxsize / plt.gcf().dpi * N + 2 * m
    margin = m / plt.gcf().get_size_inches()[0]

    plt.gcf().subplots_adjust(left=margin, right=1. - margin)
    plt.gcf().set_size_inches(s, plt.gcf().get_size_inches()[1])

    plt.savefig(figurePath)
    plt.close()